/**
 * Copyright (C) 2012 The SkyTvOS Project
 * <p>
 * Version     Date           Author
 * ─────────────────────────────────────
 * 2015-2-14         yellowlgx
 */

package com.skyworth.ui.menu;

import android.content.Context;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationSet;
import android.view.animation.OvershootInterpolator;
import android.view.animation.TranslateAnimation;
import android.widget.FrameLayout;

import com.skyworth.commen.ui.R;
import com.skyworth.ui.api.SkyCommenMenu;
import com.skyworth.ui.api.SkyViewConfig;
import com.skyworth.ui.define.SkyTextSize;
import com.skyworth.util.MyFocusFrame;
import com.skyworth.util.SkyScreenParams;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName SkyCommenMenu
 * @Description TODO (write something)
 * @author yellowlgx
 * @date 2015-2-14
 * @version TODO (write something)
 */
public class SkyFirstMenu extends FrameLayout {
    private Context mContext;
    private int menuWidth = SkyScreenParams.getInstence(mContext).getResolutionValue(250),
            menuHeight = SkyScreenParams.getInstence(mContext).getResolutionValue(572);
    private int itemHeight = SkyScreenParams.getInstence(mContext).getResolutionValue(100);
    private int itemMargin = SkyScreenParams.getInstence(mContext).getResolutionValue(18);

    private List<SkyMenuItem> menuItems;// 菜单内部项
    private SkyMenuAdapter mAdapter;// 数据adapter
    private int dataCount = 0;// 数据总数

    private int lastFocusItemID = -1;// 一级菜单负焦点ID

    private int startOffset = 160; // 动画延迟时间

    private int moveDuration = 100;

    private long onKeyTime = 0; // 判断两次点击按键的时间

    private boolean isAnimStart = false; // 动画执行标志位

    private OnItemOnkeyListener mListener;

    public boolean endDismiss = true;

    private boolean showAnimStart = false;

    private MyFocusFrame menuFocusView;

    private boolean isShowingSecondMenu = false;

    // 菜单项监听
    public interface OnItemOnkeyListener {
        public boolean onItemOnKeyLeft(int itemID, View v);

        public boolean onItemOnKeyRight(int itemID, View v, SkyMenuData currentData);

        public boolean onItemOnClick(int itemIndex, View v, SkyMenuData currentData);

        public boolean onItemOnKeyBack(int itemID, View v);

        public boolean onItemOnKeyOther(int itemID, View v, int keyCode);

        public void onFirstMenuHiddenOver();
    }

    public SkyFirstMenu(Context context) {
        super(context);
        mContext = context;
        initView();
        this.setFocusable(false);
    }

    public void setOnItemOnkeyListener(OnItemOnkeyListener listener) {
        mListener = listener;
    }

    private void initView() {
        menuItems = new ArrayList<SkyMenuItem>();
        FrameLayout.LayoutParams contentParams = new FrameLayout.LayoutParams(
                LayoutParams.WRAP_CONTENT, menuHeight);
        this.setLayoutParams(contentParams);
    }

    public void setFocusView(MyFocusFrame menuFocusView) {
        this.menuFocusView = menuFocusView;
    }

    public boolean isShowingSecondMenu() {
        Log.v("lgx", "isShowingSecondMenu--->  " + isShowingSecondMenu);
        return isShowingSecondMenu;
    }

    public void setShowingSecondMenu(boolean isShowingSecondMenu) {
        this.isShowingSecondMenu = isShowingSecondMenu;
        Log.v("lgx", "setShowingSecondMenu--->  " + isShowingSecondMenu);
    }

    /**
     * 显示一级菜单并设置一级菜单数据，默认第一个选项获得焦点
     *
     * @param adapter
     *            默认数据
     * @date 2015-3-6
     */
    public void showFitstMenu(SkyMenuAdapter adapter) {
        setMenuAdapter(adapter);
    }

    public void refreshOnThemeChanged() {
        if (menuItems != null) {
            for (int i = 0; i < menuItems.size(); i++) {
                menuItems.get(i).refreshOnThemeChanged();
            }
        }
    }

    public void refreshItemData(SkyMenuData data, int index) {
        if (menuItems != null && menuItems.size() >= index) {
            menuItems.get(index).refreshItemValue(data);
        }
    }

    private void setMenuAdapter(SkyMenuAdapter adapter) {
        this.removeAllViews();
        isAnimStart = false;
        endDismiss = true;
        showAnimStart = true;
        if (menuItems != null)
            menuItems.clear();
        if (mAdapter != null)
            mAdapter = null;

        dataCount = 0;

        mAdapter = adapter;
        if (mAdapter != null) {
            dataCount = mAdapter.getCount();
            if (dataCount <= 0) {
                return;
            }
            FrameLayout.LayoutParams itemParams = null;
            for (int i = 0; i < dataCount; i++) {
                SkyMenuItem item = new SkyMenuItem(mContext);
                item.setTextAttribute(
                        SkyScreenParams.getInstence(mContext).getTextDpiValue(SkyTextSize.t_4),
                        R.color.c_1);
                item.setFocusView(menuFocusView);
                item.setFocusable(true);
                item.setFocusableInTouchMode(true);
                item.setSelectState(false);
                item.setId(i);
                item.setOnKeyListener(itemOnKeyListener);
                // item.setOnClickListener(itemClickListener);
                item.setOnTouchListener(itemTouchListener);
                itemParams = new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT, itemHeight);
                itemParams.topMargin = (i + 2) * (itemHeight + itemMargin);
                if (itemParams != null)
                    item.setLayoutParams(itemParams);
                item.setVisibility(View.INVISIBLE);
                item.updataItemValue(mAdapter.getMenuItemData(i));
                menuItems.add(item);
                this.addView(item);
            }
            showAnimation();
        }
    }

    private void showAnimation() {
        this.post(new Runnable() {
            @Override
            public void run() {
                int size = 0;
                if (dataCount > 3) {
                    size = 3;
                } else {
                    size = dataCount;
                }
                for (int i = 0; i < size; i++) {
                    // menuItems.get(i).clearAnimation();
                    AnimationSet animset = new AnimationSet(false);
                    TranslateAnimation transAnimation = new TranslateAnimation(
                            -SkyFirstMenu.this.getWidth(), 0, 0, 0);
                    transAnimation.setStartOffset(((i) * 150) / 3);
                    transAnimation.setDuration(100);
                    transAnimation.setInterpolator(new OvershootInterpolator(1.0f));
                    // transAnimation.setInterpolator(new AccelerateInterpolator());
                    animset.addAnimation(transAnimation);

                    AlphaAnimation alphInAnimation = new AlphaAnimation(0, 1);
                    alphInAnimation.setStartOffset(((i) * 150) / 3);
                    alphInAnimation.setDuration(100);
                    alphInAnimation.setInterpolator(new AccelerateInterpolator());
                    animset.addAnimation(alphInAnimation);
                    menuItems.get(i).setVisibility(View.VISIBLE);
                    menuItems.get(i).startAnimation(animset);
                    if (i == (0))
                        animset.setAnimationListener(new AnimationListener() {
                            @Override
                            public void onAnimationStart(Animation animation) {
                            }

                            @Override
                            public void onAnimationRepeat(Animation animation) {
                            }

                            @Override
                            public void onAnimationEnd(Animation animation) {
                                if (menuItems != null && menuItems.size() > 0) {
                                    menuItems.get(0).post(new Runnable() {
                                        @Override
                                        public void run() {
                                            initMenuFocusViewPosition();

                                            if (menuFocusView.getVisibility() != View.VISIBLE)
                                                menuFocusView.setVisibility(View.VISIBLE);
                                            AlphaAnimation alphInAnimation = new AlphaAnimation(0,
                                                    1);
                                            alphInAnimation.setDuration(100);
                                            menuFocusView.startAnimation(alphInAnimation);

                                            // final boolean focus =
                                            // menuItems.get(0).requestFocus();
                                            // Log.v("lgx", "focus == " + focus);
                                            Log.v("lgx",
                                                    "hasFocus == " + menuItems.get(0).hasFocus());
                                            if (!menuItems.get(0).hasFocus()) {
                                                setItemFocusble(false, 0, true);
                                                final boolean focus = menuItems.get(0)
                                                        .requestFocus();
                                                Log.v("lgx", "focus == " + focus);
                                            }
                                            showAnimStart = false;
                                        }
                                    });
                                }
                            }
                        });
                }
                // FrameLayout.LayoutParams lp = (LayoutParams) SkyCommenMenu.menuFocusView
                // .getLayoutParams();
                // lp.width = menuItems.get(0).getWidth() +
                // SkyScreenParams.getInstence(mContext).getResolutionValue(47) *
                // 2;
                // lp.height = menuItems.get(0).getHeight() +
                // SkyScreenParams.getInstence(mContext).getResolutionValue(47)
                // * 2;
                // lp.topMargin = SkyScreenParams.getInstence(mContext).getResolutionValue(603) -
                // lp.height / 2;
                // SkyCommenMenu.menuFocusView.setLayoutParams(lp);

            }
        });
    }


    public void setItemFocusble(boolean isAll, int id, boolean focusble) {
        if (isAll) {
            for (int i = 0; i < dataCount; i++) {
                menuItems.get(i).setFocusable(focusble);
            }
        } else {
            if (id < dataCount && menuItems.get(id) != null) {
                menuItems.get(id).setFocusable(focusble);
            }
        }
    }

    // show出二级菜单时，一级各个选项设置状态
    public void setItemStateShowSecond(boolean show) {
        int index = lastFocusItemID;
        int start = 0, end = 0;
        if ((index - 2) >= 0) {
            start = index - 2;
        } else {
            start = 0;
        }
        if ((index + 2) <= (menuItems.size() - 1)) {
            end = index + 3;
        } else {
            end = menuItems.size();
        }
        List<SkyMenuItem> targetItems = menuItems.subList(start, end);
        for (int i = 0; i < targetItems.size(); i++) {
            targetItems.get(i).setItemStateForSecond(show);
        }
    }

    // 二级菜单消失动画
    public void dismissAnimtion(final SkyCommenMenu menu, int index) {
        endDismiss = false;
        AlphaAnimation focusAnimation = new AlphaAnimation(1, 0);
        focusAnimation.setFillAfter(true);
        focusAnimation.setDuration(100);
        menuFocusView.startAnimation(focusAnimation);

        int start = 0, end = 0;
        if ((index - 2) >= 0) {
            start = index - 2;
        } else {
            start = 0;
        }
        if ((index + 2) <= (menuItems.size() - 1)) {
            end = index + 3;
        } else {
            end = menuItems.size();
        }
        List<SkyMenuItem> targetItems = menuItems.subList(start, end);
        if (targetItems != null) {
            int count = targetItems.size();
            for (int i = 0; i < count; i++) {
                targetItems.get(i).clearAnimation();
                AnimationSet animset = new AnimationSet(false);
                animset.setFillAfter(true);
                TranslateAnimation transAnimation = new TranslateAnimation(0,
                        -SkyFirstMenu.this.getWidth(), 0, 0);
                transAnimation.setStartOffset(((i) * 50) / count);
                transAnimation.setDuration(100);
                transAnimation.setInterpolator(new AccelerateInterpolator());
                animset.addAnimation(transAnimation);

                AlphaAnimation alphInAnimation = new AlphaAnimation(1, 0);
                alphInAnimation.setStartOffset(((i) * 50) / count);
                alphInAnimation.setDuration(100);
                alphInAnimation.setInterpolator(new AccelerateInterpolator());
                animset.addAnimation(alphInAnimation);
                targetItems.get(i).startAnimation(animset);
                if (i == (count - 1)) {
                    animset.setAnimationListener(new AnimationListener() {
                        @Override
                        public void onAnimationStart(Animation animation) {
                        }

                        @Override
                        public void onAnimationRepeat(Animation animation) {
                        }

                        @Override
                        public void onAnimationEnd(Animation animation) {
                            menu.setVisibility(View.GONE);
                            endDismiss = true;
                            if (mListener != null)
                                mListener.onFirstMenuHiddenOver();
                        }
                    });
                }
            }
        }
    }

    OnTouchListener itemTouchListener = new OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                if (isAnimStart) {
                    return true;
                }
                ((SkyMenuItem) v).setSelectState(true);
                if (mListener != null)
                    mListener.onItemOnClick(v.getId(), v, ((SkyMenuItem) v).getItemData());
                return true;
            }
            return false;
        }
    };

    // 点击监听
    // OnClickListener itemClickListener = new OnClickListener()
    // {
    // @Override
    // public void onClick(View v)
    // {
    // if (isAnimStart)
    // {
    // return;
    // }
    // ((SkyMenuItem) v).setSelectState(true);
    // if (mListener != null)
    // mListener.onItemOnClick(v.getId(), v, ((SkyMenuItem) v).getItemData());
    // }
    // };

    // 还原按键监听
    public void resetOnKeyListener() {
        for (int j = 0; j < menuItems.size(); j++) {
            menuItems.get(j).setOnKeyListener(null);
        }
    }

    // 设置菜单焦点
    public void setMenuItemFocus(int id) {
        Log.v("lgx", "back focus!!!!!  id == " + id);
        if (id == -1) {
            id = 0;
            setMenuAdapter(mAdapter);
        } else {
            if (menuItems != null && id < menuItems.size() && menuItems.get(id) != null) {
                setItemFocusble(false, id, true);
                menuItems.get(id).resetvIconBgState();
                boolean focus = menuItems.get(id).requestFocus();

                initMenuFocusViewPosition();
                Log.v("lgx", "focus == " + focus);
            }
        }
    }

    private void initMenuFocusViewPosition() {
        int x = SkyScreenParams.getInstence(mContext).getResolutionValue(22);
        int y = SkyScreenParams.getInstence(mContext).getResolutionValue(548);
        int width = SkyScreenParams.getInstence(mContext).getResolutionValue(355)
                + SkyScreenParams.getInstence(mContext).getResolutionValue(SkyViewConfig.FOCUS_VIEW_SHADER_OFFSET) * 2;
        int height = SkyScreenParams.getInstence(mContext).getResolutionValue(100)
                + SkyScreenParams.getInstence(mContext).getResolutionValue(SkyViewConfig.FOCUS_VIEW_SHADER_OFFSET) * 2;

        menuFocusView.initStarPosition(x, y, width, height);
    }

    // 设置负焦点ID
    public void setLastFocusItemID(int id) {
        Log.v("lgx", "setLastFocusItemID == " + id);
        lastFocusItemID = id;
    }

    // 获取负焦点ID
    public int getLastFocusID() {
        return lastFocusItemID;
    }

    OnKeyListener itemOnKeyListener = new OnKeyListener() {
        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            if (event.getAction() == KeyEvent.ACTION_DOWN) {
                for (int i = 0; i < dataCount; i++) {
                    menuItems.get(i).setSelectState(false);
                }

                if (!isShowingSecondMenu())
                    lastFocusItemID = -1;

                int id = v.getId();
                if (id < 0)
                    id = 0;
                if (keyCode == KeyEvent.KEYCODE_DPAD_UP) {
                    long upTime = System.currentTimeMillis();
                    if ((upTime - onKeyTime) < 150) {
                        startOffset = 60;
                        moveDuration = 60;
                    } else {
                        startOffset = 160;
                        moveDuration = 100;
                    }
                    onKeyTime = System.currentTimeMillis();
                    if (isAnimStart) {
                        return true;
                    }
                    if (isShowingSecondMenu()) {
                        return true;
                    }
                    if (mListener != null)
                        mListener.onItemOnKeyOther(id, v, keyCode);
                    if (id != 0) {
                        menuFocusView.needAnimtion(false);
                        onKeyItemMoveEvent(keyCode, v);
                        return true;
                    } else
                        return true;
                } else if (keyCode == KeyEvent.KEYCODE_DPAD_DOWN) {
                    long downTime = System.currentTimeMillis();
                    Log.v("xw", "downTime == " + downTime);
                    Log.v("xw", "onKeyTime == " + onKeyTime);
                    Log.v("xw", "(downTime - onKeyTime) == " + (downTime - onKeyTime));
                    if ((downTime - onKeyTime) < 150) {
                        startOffset = 60;
                        moveDuration = 60;
                    } else {
                        startOffset = 160;
                        moveDuration = 100;
                    }
                    onKeyTime = System.currentTimeMillis();
                    if (isAnimStart) {
                        return true;
                    }
                    if (isShowingSecondMenu()) {
                        return true;
                    }
                    menuFocusView.clearAnimation();
                    menuFocusView.setVisibility(View.VISIBLE);
                    showAnimStart = false;
                    if (mListener != null)
                        mListener.onItemOnKeyOther(id, v, keyCode);
                    if (id != dataCount - 1) {
                        menuFocusView.needAnimtion(false);
                        onKeyItemMoveEvent(keyCode, v);
                        return true;
                    } else
                        return true;
                } else if (keyCode == KeyEvent.KEYCODE_DPAD_LEFT) {
                    if (isAnimStart) {
                        return true;
                    }
                    if (showAnimStart) {
                        return true;
                    }
                    if (isShowingSecondMenu()) {
                        return true;
                    }
                    if (mListener != null) {
                        return mListener.onItemOnKeyLeft(id, v);
                    } else {
                        return true;
                    }
                } else if (keyCode == KeyEvent.KEYCODE_DPAD_RIGHT) {
                    if (isAnimStart) {
                        return true;
                    }
                    if (showAnimStart) {
                        return true;
                    }
                    if (isShowingSecondMenu()) {
                        return true;
                    }
                    if (mListener != null) {
                        SkyMenuData menuData = ((SkyMenuItem) v).getItemData();

                        menuFocusView.needAnimtion(false);
                        lastFocusItemID = id;
                        return mListener.onItemOnKeyRight(id, v, menuData);
                    } else {
                        return true;
                    }
                } else if (keyCode == KeyEvent.KEYCODE_BACK) {
                    if (isAnimStart) {
                        return true;
                    }
                    if (mListener != null) {
                        return mListener.onItemOnKeyBack(id, v);
                    } else {
                        return false;
                    }
                } else if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER
                        || keyCode == KeyEvent.KEYCODE_ENTER) {
                    if (isAnimStart) {
                        return true;
                    }
                    if (showAnimStart) {
                        return true;
                    }
                    if (isShowingSecondMenu()) {
                        return true;
                    }
                    SkyMenuData menuData = ((SkyMenuItem) v).getItemData();

                    ((SkyMenuItem) v).setSelectState(true);
                    if (mListener != null) {
                        lastFocusItemID = id;
                        mListener.onItemOnClick(id, v, menuData);
                        Log.v("lgx", "lastFocusItemID ----------=== " + lastFocusItemID);
                        return true;
                    } else {
                        return true;
                    }
                } else {
                    if (mListener != null)
                        return mListener.onItemOnKeyOther(id, v, keyCode);
                    else
                        return false;
                }
            } else if (event.getAction() == KeyEvent.ACTION_UP) {
                return true;
            }
            return false;
        }
    };

    // 上下键滚动监听和执行
    private void onKeyItemMoveEvent(int keyCode, View v) {
        isAnimStart = true;
        final int index = v.getId();
        final int moveY = itemHeight + itemMargin;
        TranslateAnimation transAnimation = null;
        AlphaAnimation alphInAnimation = null;
        AlphaAnimation alphOutAnimation = null;
        AnimationSet animset = null;
        final int itemCount = mAdapter.getCount();
        // 按上键，所有Item需要往下移动
        if (keyCode == KeyEvent.KEYCODE_DPAD_UP) {
            for (int i = itemCount - 1; i >= 0; i--) {
                final SkyMenuItem item = menuItems.get(i);

                FrameLayout.LayoutParams item_p = (LayoutParams) item.getLayoutParams();
                item_p.topMargin += moveY;
                item.setLayoutParams(item_p);
                item.clearAnimation();

                animset = new AnimationSet(false);
                transAnimation = new TranslateAnimation(0, 0, -moveY, 0);
//                transAnimation.setStartOffset(((itemCount - 1 - i) * startOffset) / itemCount);
                transAnimation.setDuration(moveDuration);
                // transAnimation.setInterpolator(new OvershootInterpolator(1.5f));
                transAnimation.setInterpolator(new AccelerateInterpolator());
                animset.addAnimation(transAnimation);

                if (item.getVisibility() != View.VISIBLE)
                    item.setVisibility(View.VISIBLE);

                if (i == index - 3) {
                    alphInAnimation = new AlphaAnimation(0, 1);
                    alphInAnimation.setFillAfter(true);
                    alphInAnimation.setDuration(moveDuration);
//                    alphInAnimation.setStartOffset((i * startOffset) / itemCount);
                    animset.addAnimation(alphInAnimation);
                }

                if (i == index + 2) {
                    alphOutAnimation = new AlphaAnimation(1, 0);
                    alphOutAnimation.setFillAfter(true);
                    alphOutAnimation.setDuration(moveDuration);
//                    alphOutAnimation
//                            .setStartOffset(((itemCount - 1 - i) * startOffset) / itemCount);
                    animset.addAnimation(alphOutAnimation);
                }
                item.startAnimation(animset);
                if (i == 0) {
                    transAnimation.setAnimationListener(new AnimationListener() {
                        @Override
                        public void onAnimationStart(Animation animation) {
                        }

                        @Override
                        public void onAnimationRepeat(Animation animation) {
                        }

                        @Override
                        public void onAnimationEnd(Animation animation) {
                            if ((index + 2) < itemCount && (index + 2) < menuItems.size()
                                    && menuItems.get(index + 2) != null) {
                                menuItems.get(index + 2).setVisibility(View.INVISIBLE);
                            }
                            if ((index - 1) == 0) {
                                setItemFocusble(false, index - 1, true);
                                menuItems.get(index - 1).requestFocus();
                            }
                            isAnimStart = false;
                        }
                    });
                } else if (i == index - 1) {
                    transAnimation.setAnimationListener(new AnimationListener() {
                        @Override
                        public void onAnimationStart(Animation animation) {
                        }

                        @Override
                        public void onAnimationRepeat(Animation animation) {
                        }

                        @Override
                        public void onAnimationEnd(Animation animation) {
                            setItemFocusble(false, index - 1, true);
                            menuItems.get(index - 1).requestFocus();
                        }
                    });
                }
            }
            // 按下键，所有Item需要往上移动
        } else if (keyCode == KeyEvent.KEYCODE_DPAD_DOWN) {
            for (int i = 0; i < itemCount; i++) {
                final SkyMenuItem item = menuItems.get(i);

                FrameLayout.LayoutParams item_p = (LayoutParams) item.getLayoutParams();
                item_p.topMargin -= moveY;
                item.setLayoutParams(item_p);
                item.clearAnimation();

                animset = new AnimationSet(false);
                transAnimation = new TranslateAnimation(0, 0, moveY, 0);
//                transAnimation.setStartOffset((i * startOffset) / itemCount);
                transAnimation.setDuration(moveDuration);
                transAnimation.setInterpolator(new AccelerateInterpolator());
                animset.addAnimation(transAnimation);

                if (item.getVisibility() != View.VISIBLE)
                    item.setVisibility(View.VISIBLE);

                if (i == index - 2) {
                    alphOutAnimation = new AlphaAnimation(1, 0);
                    alphOutAnimation.setFillAfter(true);
                    alphOutAnimation.setDuration(moveDuration);
//                    alphOutAnimation.setStartOffset((i * startOffset) / itemCount);
                    animset.addAnimation(alphOutAnimation);
                }

                if (i == index + 3) {
                    alphInAnimation = new AlphaAnimation(0, 1);
                    alphInAnimation.setFillAfter(true);
                    alphInAnimation.setDuration(moveDuration);
//                    alphInAnimation.setStartOffset(((itemCount - 1 - i) * startOffset) / itemCount);
                    animset.addAnimation(alphInAnimation);
                }

                item.startAnimation(animset);

                if (i == (itemCount - 1)) {
                    transAnimation.setAnimationListener(new AnimationListener() {
                        @Override
                        public void onAnimationStart(Animation animation) {
                        }

                        @Override
                        public void onAnimationRepeat(Animation animation) {
                        }

                        @Override
                        public void onAnimationEnd(Animation animation) {
                            if ((index - 2) >= 0 && menuItems.get(index - 2) != null) {
                                menuItems.get(index - 2).setVisibility(View.INVISIBLE);
                            }
                            if ((index + 1 == itemCount - 1)) {
                                setItemFocusble(false, index + 1, true);
                                menuItems.get(index + 1).requestFocus();
                            }
                            isAnimStart = false;
                        }
                    });
                } else if (i == (index + 1)) {
                    transAnimation.setAnimationListener(new AnimationListener() {
                        @Override
                        public void onAnimationStart(Animation animation) {
                        }

                        @Override
                        public void onAnimationRepeat(Animation animation) {
                        }

                        @Override
                        public void onAnimationEnd(Animation animation) {
                            setItemFocusble(false, index + 1, true);
                            menuItems.get(index + 1).requestFocus();
                        }
                    });
                }
            }
        }

    }
}
